package com.xing.config.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * Created by Bradish7Y on 2017/9/4.
 * 通过rabbitAdmin自动创建队列
 */
@Configuration
public class TopicRabbitConfig {

    @Resource
    private RabbitAdmin rabbitAdmin;

    final static String message = "topic.message";
    final static String messages = "topic.messages";

    @Bean
    public Queue queueMessage() {
        Queue q = new Queue(TopicRabbitConfig.message);
        rabbitAdmin.declareQueue(q);
        return q;
    }

    @Bean
    public Queue queueMessages() {
        Queue q = new Queue(TopicRabbitConfig.messages);
        rabbitAdmin.declareQueue(q);
        return q;
    }

    @Bean
    TopicExchange exchange() {
        TopicExchange t = new TopicExchange("exchange");
        rabbitAdmin.declareExchange(t);
        return t;
    }

    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        Binding b = BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
        rabbitAdmin.declareBinding(b);
        return b;
    }

    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        Binding b = BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
        rabbitAdmin.declareBinding(b);
        return b;
    }

}
